package com.everyline.baselibrary.plate

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.everyline.baselibrary.R
import com.everyline.baselibrary.plate.adapter.CharAdapter
import com.everyline.baselibrary.plate.bean.PlateItem
import java.util.ArrayList

/**
 * Created by on 2021/2/1.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

class PlateInputView(private val context: Context, val listener: (word: String) -> Unit) :
    PopupWindow(context) {

    companion object {
        const val DELETE = "delete"
    }

    private val charAdapter by lazy {
        CharAdapter(null).apply {
            addChildClickViewIds(
                R.id.mIvDelete,
                R.id.mTvWord
            )
        }
    }
    private val provinceAdapter by lazy {
        CharAdapter(null).apply {
            addChildClickViewIds(R.id.mTvWord)
        }
    }

    private val chinese = arrayOf("港", "澳", "学", "领")
    private val num = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")

    //    private val letters = arrayOf(
//        "A", "B", "C", "D", "E",
//        "F", "G", "H", "J", "K",
//        "L", "M", "N", "O", "P",
//        "Q", "R", "S", "T", "U",
//        "V", "W", "X", "Y", "Z"
//    )
//    private val letters = arrayOf(
//        "Q", "W", "E", "R", "T",
//        "Y", "U", "I", "O", "P",
//        "A", "S", "D", "F", "G",
//        "H", "J", "K", "L", "Z",
//        "X", "C", "V", "B", "N", "M"
//    )

    private val letters = arrayOf(
        "Q", "W", "E", "R", "T",
        "Y", "U",  "P",
        "A", "S", "D", "F", "G",
        "H", "J", "K", "L", "Z",
        "X", "C", "V", "B", "N", "M"
    )

    //    private val provinces = arrayOf(
//        "粤", "京", "沪", "津", "浙",
//        "苏", "湘", "赣", "鄂", "豫",
//        "皖", "陕", "甘", "宁", "云",
//        "贵", "川", "渝", "黑", "吉",
//        "辽", "晋", "冀", "鲁", "闽",
//        "琼", "桂", "蒙", "青", "藏",
//        "新"
//    )
    private val provinces = arrayOf(
        "京", "豫", "苏", "冀", "鄂",
        "津", "晋", "蒙", "辽", "黑",
        "沪", "浙", "皖", "闽", "粤",
        "桂", "琼", "赣", "湘", "甘",
        "青", "陕", "藏", "云", "贵",
        "新", "宁", "川", "鲁", "吉",
        "渝"
    )

    private val charItems by lazy { initCharItemData() }
    private val provinceItems by lazy { initProvinceItemData() }

    private fun initProvinceItemData(): ArrayList<PlateItem> {

        return ArrayList<PlateItem>().apply {

            for (index in provinces.indices) {
                add(PlateItem(PlateItem.CHAR, provinces[index]))
            }

//            (0..9).forEach { add(PlateItem(PlateItem.CHAR, provinces[it])) }
//            add(PlateItem(PlateItem.EMPTY))
//            (11..18).forEach { add(PlateItem(PlateItem.CHAR, provinces[it])) }
//            add(PlateItem(PlateItem.EMPTY))
//            add(PlateItem(PlateItem.EMPTY))
//            (23..30).forEach { add(PlateItem(PlateItem.CHAR, provinces[it])) }
        }
    }

    private fun initCharItemData(): ArrayList<PlateItem> {
        return ArrayList<PlateItem>().apply {
            num.forEach { add(PlateItem(PlateItem.CHAR, it)) }
            letters.forEach { add(PlateItem(PlateItem.CHAR, it)) }
//            (0..3).forEach { _ -> add(PlateItem(PlateItem.EMPTY)) }
//            chinese.forEach { add(PlateItem(PlateItem.CHAR, it)) }
//            (0..3).forEach { _ -> add(PlateItem(PlateItem.EMPTY)) }
            add(PlateItem(PlateItem.DELETE))
        }

    }


    init {
        initPlateView()
        initData()
        initRecyclerView()
    }

    private fun initRecyclerView() {
        contentView.findViewById<RecyclerView>(R.id.mRvContent).run {
            adapter = charAdapter


            layoutManager = GridLayoutManager(context, 10).apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int {

                        return getSpanCount(position)
                    }

                }
            }
        }

        contentView.findViewById<RecyclerView>(R.id.mRvProvince).run {
            layoutManager = GridLayoutManager(context, 10)
            adapter = provinceAdapter
        }

        charAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.mIvDelete -> {
                    listener(DELETE)
                }
                R.id.mTvWord -> {
                    listener(charAdapter.data[position].data)
                }
            }
        }

        provinceAdapter.setOnItemChildClickListener { adapter, view, position ->
            listener(provinceAdapter.data[position].data)
        }


    }

    private fun getSpanCount(position: Int): Int {
        val type = charAdapter.getItemViewType(position)
        return if (type == PlateItem.DELETE) {
            return 2
        } else {
            1
        }
    }

    private fun initData() {
        provinceAdapter.setList(provinceItems)
        charAdapter.setList(charItems)


    }

    private fun initPlateView() {
        contentView = LayoutInflater.from(context).inflate(R.layout.layout_plate_input_view, null)


        width = WindowManager.LayoutParams.MATCH_PARENT
        height = WindowManager.LayoutParams.WRAP_CONTENT
        setBackgroundDrawable(ColorDrawable(Color.WHITE))
        isFocusable = false
        isOutsideTouchable = true
        // 设置为true之后，PopupWindow内容区域 才可以响应点击事件
        isTouchable = true

    }

    fun showProvince() {
        contentView.findViewById<RecyclerView>(R.id.mRvProvince).visibility = View.VISIBLE
        contentView.findViewById<RecyclerView>(R.id.mRvContent).visibility = View.GONE
    }

    fun showPlateChar() {
        contentView.findViewById<RecyclerView>(R.id.mRvProvince).visibility = View.GONE
        contentView.findViewById<RecyclerView>(R.id.mRvContent).visibility = View.VISIBLE
    }
}